<?php
namespace app\order\service;
/**
 * 地址接口
 */
class AddressService extends \app\base\service\BaseService {

    private $model = 'order/OrderAddress';

    public function getAddress($userId, $id = 0) {
        $where = [];
        $where['user_id'] = $userId;
        if(empty($id)) {
            $where['default'] = 1;
        }else {
            $where['add_id'] = $id;
        }
        return target('order/OrderAddress')->getWhereInfo($where);
    }

    public function add($userId, $data = []) {
        $data['user_id'] = $userId;
        $count = target($this->model)->countList(['user_id' => $userId]);
        if($count >= 10) {
            return $this->error('收货地址已上限,清先删除或修改其他地址!');
        }
        if(!$count) {
            $data['default'] = 1;
        }
        if(empty($data['name']) || empty($data['tel']) || empty($data['province']) || empty($data['city']) || empty($data['region']) || empty($data['address']) || empty($data['zip'])) {
            return $this->error('收货信息填写不完整!');
        }
        target($this->model)->beginTransaction();
        if($data['default'] && $count) {
            $status = target($this->model)->where([
                'user_id' => $userId
            ])->data([
                'default' => 0
            ])->update();
            if($status === false) {
                target($this->model)->rollBack();
                return $this->error(target($this->model)->getError());
            }
        }
        if(!target($this->model)->add($data)) {
            target($this->model)->rollBack();
            return $this->error(target($this->model)->getError());
        }
        target($this->model)->commit();
        return $this->success();
    }

    public function edit($userId, $data = []) {
        $data['user_id'] = $userId;
        if(empty($data['add_id'])) {
            return $this->error('地址数据获取错误!');
        }
        if(empty($data['name']) || empty($data['tel']) || empty($data['province']) || empty($data['city']) || empty($data['region']) || empty($data['address']) || empty($data['zip'])) {
            return $this->error('收货信息填写不完整!');
        }
        $info = target('order/OrderAddress')->getWhereInfo([
            'add_id' => $data['add_id'],
            'user_id' => $userId
        ]);
        if(empty($info)) {
            return $this->error('该地址不存在或已删除!');
        }
        $count = target($this->model)->countList(['user_id' => $userId]);
        if($count <= 1) {
            $data['default'] = 1;
        }
        target($this->model)->beginTransaction();
        if($data['default'] && $count > 1) {
            $status = target($this->model)->where([
                'user_id' => $userId
            ])->data([
                'default' => 0
            ])->update();
            if($status === false) {
                target($this->model)->rollBack();
                return $this->error(target($this->model)->getError());
            }
        }
        if(!target($this->model)->edit($data)) {
            target($this->model)->rollBack();
            return $this->error(target($this->model)->getError());
        }
        target($this->model)->commit();
        return $this->success();
    }

    public function del($userId, $id) {
        $count = target($this->model)->countList(['user_id' => $userId]);
        if($count <= 1) {
            return $this->error('无法删除最后一个地址!');
        }
        $info = target('order/OrderAddress')->getWhereInfo([
            'add_id' => $id,
            'user_id' => $userId
        ]);
        if(empty($info)) {
            return $this->error('该地址不存在或已删除!');
        }
        if(!target($this->model)->del($id)) {
            return $this->error(target($this->model)->getError());
        }
        return $this->success();
    }

}
